package com;

import java.io.IOException;
import java.io.PrintWriter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.IUserDao;
import com.dao.UserDaoImp;
import com.vo.Users;

public class ModifyAssignment extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html,utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		int id = 0,ids = 0;
		id  = Integer.parseInt(request.getParameter("ID"));
		IUserDao dao = new UserDaoImp();
		List<Users> list = new ArrayList<Users>();
		String username = null,pwd=null,sex=null,birth=null,mobile=null,address=null;
		int birthy = 0,birthm = 0,birthd =0;
		try {
			list = dao.getAllData();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i = 0;i < list.size();i++){
			Users users = list.get(i);
			//System.out.println("Lsit"+list.size());
			ids = users.getEu_user_id();
			if(id == ids){
				username = users.getEu_user_name();
				pwd = users.getEu_password();
				sex = users.getEu_sex();
				birth = users.getEu_birthday();
				//System.out.println(username+pwd+sex+birth+mobile+address);
				mobile = users.getEu_mobile();
				address = users.getEu_address();
				//System.out.println(username+pwd+sex+birth+mobile+address);
			}
		}
		if(birth == null){
			birthy = 0000;
			birthm = 00;
			birthd = 00;
		}else{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd");
			try {
				Date date = format.parse(birth);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				birthy = calendar.get(Calendar.YEAR);
				birthm = calendar.get(Calendar.MONTH)+1;
				birthd = calendar.get(Calendar.DAY_OF_MONTH);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		HttpSession session = request.getSession();
		//System.out.println(username+pwd+sex+birth+mobile+address);
		session.setAttribute("uname", username);
		session.setAttribute("pwds", pwd);
		session.setAttribute("sex", sex);
		session.setAttribute("birth",birth);
		session.setAttribute("birthy", birthy);
		System.out.println(birthy+"-"+birthm+"-"+birthd);
		session.setAttribute("birthm", birthm);
		session.setAttribute("birthd", birthd);
		session.setAttribute("mobile", mobile);
		session.setAttribute("address", address);
		session.setAttribute("id", id);
		request.getRequestDispatcher("manage/user-modify.jsp").forward(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
